package com.dupf.product.rpc.service.impl;

import com.dupf.common.annotation.BaseService;
import com.dupf.common.base.BaseServiceImpl;
import com.dupf.product.dao.mapper.ProductCategoryMapper;
import com.dupf.product.dao.model.ProductCategory;
import com.dupf.product.dao.model.ProductCategoryExample;
import com.dupf.product.rpc.api.ProductCategoryService;
import com.google.common.base.Splitter;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * ProductCategoryService实现
 * Created by dupf on 2018/3/14.
 */
@Service
@Transactional
@BaseService
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategoryMapper, ProductCategory, ProductCategoryExample> implements ProductCategoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductCategoryServiceImpl.class);

    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Override
    public String getCategoryByIds(String categoryIds) {
        if (StringUtils.isEmpty(categoryIds)) {
            return null;
        }
        List<Integer> ids = Splitter.on(",").splitToList(categoryIds).stream().map(Integer::valueOf).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria().andIdIn(ids);

        List<ProductCategory> categoryList = Optional.ofNullable(productCategoryMapper.selectByExample(example)).orElse(Collections.emptyList());
        List<String> categoryNames = new ArrayList<>();
        ids.forEach(p -> {
            categoryList.forEach(t -> {
                if (p.equals(t.getId())) {
                    categoryNames.add(t.getName());
                }
            });
        });

        return StringUtils.join(categoryNames, "/");
    }
}